package com.hsnn.medstgmini.trading.pur.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.trading.pur.model.form.PurOrderLogisticsForm;
import com.hsnn.medstgmini.trading.pur.service.PurOrderLogisticsManager;
import com.hsnn.medstgmini.util.Pagination;

@Controller
@RequestMapping("/purOrderLogistics")
public class PurOrderLogisticsController {
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private PurOrderLogisticsManager purOrderLogisticsManager;

	/**
	 * 
	 * @category 跳向更新物流信息页面
	 * @author 邱磊
	 * @date 2015年7月15日 下午4:17:45
	 * @param interflowId
	 * @return
	 */
	@RequestMapping("/toUpdateLogistics")
	public String toUpdateLogistics(String interflowId) {
		PurOrderLogisticsForm form = purOrderLogisticsManager
				.getLogistics(interflowId);
		if (null == form ){
			form = new PurOrderLogisticsForm();
			form.setInterflowId(interflowId);
		}
		request.setAttribute("info", form);
		return "/trading/pur/updateLogistics";
	}

	/**
	 * 
	 * @category 更新物流信息
	 * @author 邱磊
	 * @date 2015年7月15日 下午5:33:30
	 * @param form
	 * @return
	 */
	@RequestMapping("/updateLogistics")
	@ResponseBody
	public Pagination updateLogistics(PurOrderLogisticsForm form) {
		Pagination page = new Pagination();
		SysUser user = (SysUser) request.getSession().getAttribute(
				Constants.USERINFO);
		form.setUpdUser(user.getId());
		if (StringUtils.isBlank(form.getInterflowTime())){
			page.setSuccess(false);
			page.setMsg("物流运送时间不能为空！");
			return page;
		}
		if (StringUtils.isBlank(form.getInterflowDetail())){
			page.setSuccess(false);
			page.setMsg("物流运送详情不能为空！");
			return page;
		}
		page.setForm(form);
		try {
			page = purOrderLogisticsManager.updateLogistics(page);
		} catch (Exception e) {
			page.setSuccess(false);
			page.setMsg("更新失败，请联系管理员！");
			return page;
		}
		return page;
	}
}
